package us.team.openAnAppAtRandom;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Full screen black mask layer Activity
 * Shows a full screen black mask with "Untouch" text
 * User can see but cannot interact with content below the mask
 */
public class MaskActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Set window to fullscreen and show above other apps
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
                WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
                WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS |
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        // 隐藏系统UI并设置为沉浸式模式
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                View.SYSTEM_UI_FLAG_FULLSCREEN |
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                View.SYSTEM_UI_FLAG_LOW_PROFILE |
                View.SYSTEM_UI_FLAG_IMMERSIVE);
        
        // Set window type to system overlay to show above other apps
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
        
        // Create a frame layout as root view
        FrameLayout rootLayout = new FrameLayout(this);
        rootLayout.setBackgroundColor(Color.BLACK); // Set black background
        
        // Create "Untouch" text view
        TextView untouchText = new TextView(this);
        untouchText.setText("请绘制Z字退出");
        untouchText.setTextColor(Color.argb(50, 255, 255, 255)); // Set light color text
        untouchText.setTextSize(40); // Set large font size
        
        // Set text to center
        FrameLayout.LayoutParams textParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT
        );
        textParams.gravity = Gravity.CENTER;
        
        // Add text to root layout
        rootLayout.addView(untouchText, textParams);
        
        // 添加导航栏和状态栏覆盖视图
        // 获取屏幕尺寸
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getRealSize(size); // 获取真实屏幕尺寸
        
        // 获取导航栏高度
        int navBarHeight = 0;
        int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            navBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        
        // 创建导航栏覆盖视图
        View navBarCover = new View(this);
        navBarCover.setBackgroundColor(Color.BLACK);
        
        // 根据设备方向设置导航栏覆盖层位置
        FrameLayout.LayoutParams navBarParams;
        if (size.x > size.y) { // 横屏
            navBarParams = new FrameLayout.LayoutParams(navBarHeight, size.y);
            navBarParams.gravity = Gravity.RIGHT;
        } else { // 竖屏
            navBarParams = new FrameLayout.LayoutParams(size.x, navBarHeight);
            navBarParams.gravity = Gravity.BOTTOM;
        }
        
        rootLayout.addView(navBarCover, navBarParams);
        
        // 创建状态栏覆盖视图
        int statusBarHeight = 0;
        int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (statusBarResourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(statusBarResourceId);
        }
        
        View statusBarCover = new View(this);
        statusBarCover.setBackgroundColor(Color.BLACK);
        FrameLayout.LayoutParams statusBarParams = new FrameLayout.LayoutParams(size.x, statusBarHeight);
        statusBarParams.gravity = Gravity.TOP;
        
        rootLayout.addView(statusBarCover, statusBarParams);
        
        // Set content view
        setContentView(rootLayout);
        
        // 拦截所有触摸事件，包括导航栏和状态栏区域
        rootLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 消费所有触摸事件，不让它们传递到下层
                return true;
            }
        });
    }
}